package com.gc.service.impl;

import com.gc.entity.*;
import com.gc.repository.CcsRepository;
import com.gc.repository.CourseRepository;
import com.gc.repository.TeachClassRepository;
import com.gc.repository.TeacherRepository;
import com.gc.repository.impl.CcsRepositoryImpl;
import com.gc.repository.impl.CourseRepositoryImpl;
import com.gc.repository.impl.TeachClassRepositoryImpl;
import com.gc.repository.impl.TeacherRepositoryImpl;
import com.gc.service.TeachClassService;
import com.gc.vo.TeachClassShow;
import org.checkerframework.checker.units.qual.C;


import java.util.ArrayList;
import java.util.List;

public class TeachClassServiceImpl implements TeachClassService {
    TeachClassRepository teachClassRepository = new TeachClassRepositoryImpl();
    CourseRepository courseRepository = new CourseRepositoryImpl();
    CcsRepository ccsRepository = new CcsRepositoryImpl();
    TeacherRepository teacherRepository = new TeacherRepositoryImpl();
    @Override
    public boolean Add(TeachClass teachClass) {
        return teachClassRepository.Add(teachClass);
    }

    @Override
    public List<TeachClassShow> FindAll() {
        List<TeachClass> teachClasses = teachClassRepository.FindAll();
        List<TeachClassShow> res = new ArrayList<>();
        for(TeachClass i : teachClasses) {
            Course course = courseRepository.FindByCourseId(i.getCid());
            System.out.println("courseid: " + i.getCid());
            System.out.println("course: " + course);
            Teacher teacher = teacherRepository.FindByTeacherId(i.getTid());
            System.out.println("teacherid: " + i.getTid());
            System.out.println("teacher: " + teacher);
            res.add(new TeachClassShow(
                    i.getId(),
                    i.getName(),
                    i.getClassid(),
                    i.getCid(),
                    course.getName(),
                    i.getTid(),
                    teacher.getName()
            ));
        }
        return res;
    }

    @Override
    public boolean DelById(Long id) {
        TeachClass teachClass = teachClassRepository.FindById(id);
        List<Ccs> byStudentId = ccsRepository.findByClassId(teachClass.getClassid());
        if(byStudentId != null) {
            ccsRepository.DelByClassId(teachClass.getClassid());
        }
        teachClassRepository.DelById(id);
        return true;
    }

    @Override
    public TeachClassShow FindById(Long id) {
        TeachClass teachClass = teachClassRepository.FindById(id);
        Course course = courseRepository.FindByCourseId(teachClass.getCid());
        Teacher teacher = teacherRepository.FindByTeacherId(teachClass.getTid());
        return new TeachClassShow(teachClass.getId(),
                teachClass.getName(),
                teachClass.getClassid(),
                teachClass.getCid(),
                course.getName(),
                teachClass.getTid(),
                teacher.getName());
    }

    @Override
    public boolean UpdateById(TeachClass teachClass) {
        return teachClassRepository.UpdateById(teachClass);
    }
}
